<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductsController extends Controller
{
    public function index(Request $request){
        $products=Product::where('on_sale', true);
        if($search=$request->input('search')){
            $like="%$search%";
            $products->where(function($pro) use($like){
                $pro->where('title', 'like', $like)
                    ->orWhere('description', 'like', $like)
                    ->orWhereHas('skus',function($sku) use($like){
                        $sku->where('title', 'like', $like)
                            ->orWhere('description', 'like', $like);
                    });
            });
        }
        if($order=$request->input('order')){
            if(preg_match("/(price|sold_count|review_count)_(asc|desc)/", $order, $match))
                $products->orderBy($match[1], $match[2]);
        }
        $products=$products->paginate(16);
        return view('products.index', [
            'products'=>$products,
            'filters'=>[
                'order'=>$order,
                'search'=>$search,
            ]
        ]);
    }
    public function show(Product $product, Request $request){
        $active=0;
        if($user=$request->user()){
            $favorites=$user->favoriteProducts()->find($product->id);
            $active=$favorites? 1: 0;
        }
        return view('products.show', [
            'product'=>$product,
            'active'=>$active,
        ]);
    }
    public function favor(Product $product, Request $request){
        $user=$request->user();
        $favorites=$user->favoriteProducts();
        if($favorites->find($product->id))
            return ['msg'=>'无需重复收藏', 'icon'=>'warning'];
        $favorites->attach($product);
        return ['msg'=>'收藏成功', 'icon'=>'success'];
    }
    public function disfavor(Product $product, Request $request){
        $request->user()->favoriteProducts()->detach($product);
        return ['msg'=>'已取消收藏', 'icon'=>'success'];
    }
    public function favorites(Request $request){
        $products=$request->user()->favoriteProducts()->where('on_sale', true);
        if($search=$request->input('search')){
            $like="%$search%";
            $products->where(function($pro) use($like){
                $pro->where('title', 'like', $like)
                    ->orWhere('description', 'like', $like)
                    ->orWhereHas('skus',function($sku) use($like){
                        $sku->where('title', 'like', $like)
                            ->orWhere('description', 'like', $like);
                    });
            });
        }
        if($order=$request->input('order')){
            if(preg_match("/(price|sold_count|review_count)_(asc|desc)/", $order, $match))
                $products->orderBy($match[1], $match[2]);
        }
        $products=$products->paginate(16);
        return view('products.index', [
            'products'=>$products,
            'filters'=>[
                'order'=>$order,
                'search'=>$search,
            ]
        ]);
    }
}
